package com.sheep.opt;

import com.sheep.opt.SessionManager;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.http.HttpSession;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * Created by IntelliJ IDEA.
 * User: wnj
 * Date: 2010-11-2
 * Time: 20:43:52
 * To change this template use File | Settings | File Templates.
 */
public class ThreadUtil{
    protected static final ThreadLocal<Connection> _conn_wnj = new ThreadLocal<Connection>();
    protected static final ThreadLocal<SqlSession> _sqlsession = new ThreadLocal<SqlSession>();
    protected static final ThreadLocal<HttpSession> _hsession = new ThreadLocal<HttpSession>();

	public static Connection currentConnWnj() throws SQLException {
		Connection conn = _conn_wnj.get();
        if(conn == null) {
            conn = DbConnector.getConn();
            _conn_wnj.set(conn);            
        }
        if (conn == null) {
            throw new SQLException("Can't open Connection");
        }
		return conn;
	}

	public static void closeConnWnj() throws SQLException {
		Connection conn = _conn_wnj.get();
		_conn_wnj.set(null);
		if (conn != null) {
            if (!conn.isClosed()) {
			    conn.close();
            }
		}
	}

    public static boolean beginTranx() {
		try {
			Connection conn = currentConnWnj();
			if (conn.getAutoCommit()) {
				conn.setAutoCommit(false);
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
    }

    public static boolean commitTranx(){
		try {
			Connection conn = currentConnWnj();
			if (conn.getAutoCommit()) {
				return false;
			}
			try {
				conn.commit();
				return true;
			} catch (Exception e) {
				conn.rollback();
				return false;
			} finally {
				conn.setAutoCommit(true);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
    }

    public static boolean rollbackTranx(){
        try {
            Connection conn = currentConnWnj();
            if (conn.getAutoCommit()) {
                return false;
            }

            try {
                conn.rollback();
                return true;
            } catch ( Exception e) {
                return false;
            } finally {
                conn.setAutoCommit(true);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

	public static HttpSession currentHttpSession() {
		return _hsession.get();
	}

	public static void setHttpSession(HttpSession hs) {
		_hsession.set(hs);
	}

	public static void clearHttpSession() {
		_hsession.set(null);
	}

    

    public static SqlSession getSqlSession() throws SQLException {
        SqlSession sqlSession = _sqlsession.get();
        if (sqlSession == null) {
            sqlSession = SessionManager.openSession(true);
            _sqlsession.set(sqlSession);
        }
        if (sqlSession == null) {
            throw new SQLException("Can't open ibatis session");
        }
        return sqlSession;
    }

    public static void closeSqlSession() {
        SqlSession sqlSession = _sqlsession.get();
        _sqlsession.set(null);
        if (sqlSession != null) {
            sqlSession.close();
        }
    }

    public static void closeAll() throws SQLException {
        closeSqlSession();
        clearHttpSession();        
        closeConnWnj();
    }
}
